package com.zhihesj.anan.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhihesj.anan.message.Message;
import com.zhihesj.anan.message.MessageCodeEnum;
import com.zhihesj.anan.system.entity.*;
import com.zhihesj.anan.system.service.RoleDeptService;
import com.zhihesj.anan.system.service.RoleService;
import com.zhihesj.anan.system.vo.RoleVo;
import com.zhihesj.anan.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2020-07-22
 */
@RestController
@RequestMapping("/system/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private RoleDeptService roleDeptService;

    @PostMapping("/addRole")
    public Message addRole(@RequestBody @Valid Role role){
        Map<String,Object> objectStringMap=BeanUtil.beanToMap(role, false, true);
        if (objectStringMap.isEmpty()) {
            return Message.success(MessageCodeEnum.OPERATION_ERROR);
        }
        roleService.save(role);
        return Message.success();
    }

    @GetMapping("/getRoleList")
    public Message getRoleList(RoleVo roleVo){
        System.out.println(roleVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(roleVo, false, true);
        List<Role> list=null;
        BaseUtil baseUtil = new BaseUtil();
        if (stringObjectMap.isEmpty()) {
            list=roleService.list();
            List<Map<String, Object>> maps = baseUtil.ArrangementRoleTree(list);
            return Message.success(maps);
        } else {
            stringObjectMap.put("page", ((Integer) stringObjectMap.get("page") - 1) * (Integer) stringObjectMap.get("limit"));
            list = roleService.list();
            List<Map<String, Object>> maps = baseUtil.ArrangementRoleTree(list);
            return Message.success(maps);
        }
    }

    /**
     * 修改默认角色设置
     * @param role
     * @return
     */
    @PostMapping("/updateRoleDefaultJurisdiction")
    public Message updateRoleDefaultJurisdiction(@RequestBody Role role){
        if (role.getRoleId()==1L) {
            return Message.success(MessageCodeEnum.ROLE_EDIT_ERROR);
        }
        Integer isDefault = role.getIsDefault();
        if (isDefault.equals(Integer.valueOf(1))){
            // 取消默认
            role.setIsDefault(0);
        }else if (isDefault.equals(Integer.valueOf(0))){
            // 设置默认
            role.setIsDefault(1);
            UpdateWrapper<Role> wrapper = new UpdateWrapper<>();
            wrapper.set("is_default", 0);
            roleService.update(wrapper);
            role.setIsDefault(1);
        } else {
            return Message.success(MessageCodeEnum.PARAM_ERROR);
        }
        roleService.updateById(role);
        return Message.success(MessageCodeEnum.UPDATE_SUCCESS);
    }

    @PostMapping("/updateRoleByRoleId")
    public Message updateRoleByRoleId(@RequestBody Role role){
        if (role.getRoleId()==1L){
            return Message.success(MessageCodeEnum.ROLE_EDIT_ERROR);
        }
        Role role1 = roleService.getById(role.getRoleId());
        if (role1.getIsDefault()==1){
            return Message.success(MessageCodeEnum.ROLE_NOT_DITE_DEFAULT);
        }
        if (roleService.updateById(role)){
            return Message.success(MessageCodeEnum.SUCCESS);
        }
        return Message.success(MessageCodeEnum.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByRoleId")
    public Message deleteByRoleId(@RequestBody Role role){
        if (role.getRoleId()==1L){
            return Message.success(MessageCodeEnum.ROLE_EDIT_ERROR);
        }
        if (roleService.removeById(role.getRoleId())){
            return Message.success(MessageCodeEnum.DELETE_SUCCESS);
        }
        return Message.success(MessageCodeEnum.DELETE_ERROR);
    }

    @Transactional
    @GetMapping("/fetchRoleInfo")
    public Message fetchRoleInfo(Long roleId){
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<RoleMenu> menuList =roleService.getMenuList(roleId);
        map.put("menu", menuList);
        List<UserRole> userList = roleService.getUserList(roleId);
        map.put("user",userList);
        List<RoleDept> deptList= roleService.getDeptList(roleId);
        map.put("dept", deptList);
        return Message.success(map);
    }

    @PostMapping("/editDataScope")
    public Message editDataScope(@RequestBody Map<String,Object> map){
        String str = (String) map.get("dataScope");
        Long roleId=((Integer)map.get("roleId")).longValue();
        Role role=new Role();
        role.setRoleId(roleId);
        role.setDataScope(str);
        roleService.updateById(role);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("role_id", roleId);
        roleDeptService.remove(wrapper);
        return Message.success();
    }

    @GetMapping("/getRolesByIdentifier")
    public Message getRolesByIdentifier(Users users) {
        String identifier = users.getNickName();
        if (!identifier.isEmpty()) {
            List<Role> lists = roleService.getRolesByIdentifier(identifier);
            return Message.success(lists);
        }
        return Message.success(MessageCodeEnum.ROLE_IDENTIFIERNULL_ERROR);
    }


}
